package com.atguigu.srb.core.controller.api;


import com.atguigu.common.constant.RedisConstant;
import com.atguigu.common.exception.Assert;
import com.atguigu.common.result.R;
import com.atguigu.common.result.ResponseEnum;
import com.atguigu.common.utils.RegexValidateUtils;
import com.atguigu.srb.core.pojo.vo.LoginVO;
import com.atguigu.srb.core.pojo.vo.RegisterVO;
import com.atguigu.srb.core.pojo.vo.UserInfoVO;
import com.atguigu.srb.core.service.UserInfoService;
import com.atguigu.srb.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 用户登录记录表 前端控制器
 * </p>
 *
 * @author gcq
 * @since 2021-04-11
 */
@Api(tags = "会员接口")
@RestController
@RequestMapping("/api/core/userInfo/")
public class UserLoginRecordController {

    @Autowired
    StringRedisTemplate redisTemplate;

    @Autowired
    UserInfoService userInfoService;

    /**
     * 前端会对参数做一个校验，后端参数也是需要做校验，防止用户绕过前端页面，直接向后端发起请求！
     * @param registerVo
     * @return
     */
    @ApiOperation("会员注册")
    @PostMapping("/register")
    public R register(@RequestBody RegisterVO registerVo)
    {
        String mobile = registerVo.getMobile();
        String password = registerVo.getPassword();
        String code = registerVo.getCode();

        // 手机号不能为空
        Assert.notEmpty(mobile, ResponseEnum.MOBILE_NULL_ERROR);
        // 密码不能为空
        Assert.notEmpty(password, ResponseEnum.PASSWORD_NULL_ERROR);
        // 验证码不能为空
        Assert.notEmpty(code, ResponseEnum.MOBILE_NULL_ERROR);
        // 验证手机号格式是否正确 手机号验证格式需要进行更新
        Assert.isTrue(RegexValidateUtils.checkCellphone(mobile), ResponseEnum.MOBILE_ERROR);

        String redisCode = (String) redisTemplate.opsForValue().get(RedisConstant.SRB_SMS_CODE + registerVo.getMobile());
        // redis中的验证码与用户提交的验证码是否相等，如果不相等抛出异常
        Assert.equals(redisCode, code , ResponseEnum.CODE_ERROR);

        userInfoService.register(registerVo);
        return R.ok().message("注册成功");
    }

    @ApiOperation("会员登录")
    @PostMapping("/login")
    public R login(@RequestBody LoginVO loginVO, HttpServletRequest request)
    {
        String mobile = loginVO.getMobile();
        String password = loginVO.getPassword();

        // 手机号和密码不能为空
        Assert.notEmpty(mobile, ResponseEnum.MOBILE_NULL_ERROR);
        Assert.notEmpty(password, ResponseEnum.PASSWORD_NULL_ERROR);

        UserInfoVO userInfoVO = userInfoService.login(loginVO, request.getRemoteAddr());

        return R.ok().data("userInfo", userInfoVO);
    }

    @ApiOperation("校验header中的token是否有效")
    @GetMapping("/checkToKen")
    public R checkToken(HttpServletRequest request)
    {
        String token = request.getHeader("token");
        return JwtUtils.checkToken(token) ? R.ok() : R.setResult(ResponseEnum.LOGIN_AUTH_ERROR);
    }

    @GetMapping("/checkMobile/{mobile}")
    public boolean checkMobile(@PathVariable("mobile") String mobile)
    {
        return userInfoService.checkMobile(mobile);
    }

}